package com.mall.shopping.services;

import com.mall.shopping.IProductCateService;
import com.mall.shopping.constants.ShoppingRetCode;
import com.mall.shopping.converter.ContentConverter;
import com.mall.shopping.converter.ProductCateConverter;
import com.mall.shopping.dal.entitys.ItemCat;
import com.mall.shopping.dal.entitys.PanelContent;
import com.mall.shopping.dal.persistence.ItemCatMapper;
import com.mall.shopping.dal.persistence.PanelContentMapper;
import com.mall.shopping.dto.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author 晓帆
 * @version 1.0
 **/
@Service
@Slf4j
public class IProductCateServiceImpl implements IProductCateService {
    /**
     * 获取所有商品分类
     * @param request
     * @return
     */
        @Autowired
    ItemCatMapper itemCatMapper;
        @Autowired
    ProductCateConverter productCateConverter;
        @Autowired
    PanelContentMapper panelContentMapper;
        @Autowired
    ContentConverter converter;

    /**
     * 获得所有的分类
     * @param request request
     * @return AllProductCateResponse
     */
    @Override
    public AllProductCateResponse getAllProductCate(AllProductCateRequest request) {
        AllProductCateResponse allProductCateResponse = new AllProductCateResponse();
        try {
            request.requestCheck();
            List<ItemCat> itemCats = itemCatMapper.selectAll();
            List<ProductCateDto> productCateDtos = productCateConverter.items2Dto(itemCats);
            allProductCateResponse.setProductCateDtoList(productCateDtos);
            allProductCateResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            allProductCateResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return allProductCateResponse;
    }

    /**
     * 获取板块信息
     * @return NavListResponse
     */
    @Override
    public NavListResponse getAllNav() {
        NavListResponse navListResponse = new NavListResponse();
        try {
            Example example = new Example(PanelContent.class);
            example.createCriteria().andEqualTo("panelId",0);
            List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
            List<PanelContentDto> panelContentDtos = converter.panelContents2Dto(panelContents);
            navListResponse.setPannelContentDtos(panelContentDtos);
            navListResponse.setCode(ShoppingRetCode.SUCCESS.getCode());
            navListResponse.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
        }
        return navListResponse;
    }
}
